<?php 
/**
 * 列表页控制器
 */
class ListController extends CommonController{
	
	public function index(){
		//获取列表页数据
		$cid = Q('get.cid',0,'intval');
		//获取分类标题
		$cateDatas = K('Category')->where("cid={$cid}")->find();
		$this->assign('cateDatas',$cateDatas);
		$cate = K('Category')->all();
		
		//获取cid及其子集
		$cids = K('Category')->getSon($cate,$cid);
		$cids[] = $cid;
		$cids = implode(',', $cids);

		//处理筛选
		
		//获取顶级分类id
		$topCids = K('Category')->where("pid=0")->getField('cid',true);
		//获取分类下的所有商品id
		$gids = K('Goods')->where("category_cid IN(" . $cids . ")")->getField('gid',true);
		//判断是否是顶级分类，顶级分类不用进行筛选
		if(!in_array($cid, $topCids)){
			//通过该分类下的所有的商品属性来获取筛选属性（非当前分类下的类型属性）
			if($gids){
				$goodsAttr = K('GoodsAttr')->where("goods_gid IN (" . implode(',', $gids) . ")")->group('gvalue')->all();
				//从组属性数组
				$tempAttr = array();
				foreach ($goodsAttr as $v) {
					$tempAttr[$v['type_attr_taid']][] = $v;
				}
				//获取含有属性名称的属性数组
				$attr = array();
				foreach ($tempAttr as $k => $v) {
					$attr[] = array(
						'name'  => K('TypeAttr')->where("taid={$k}")->getField('taname'),
						'value' => $v
					);
				}
				$this->assign('attr',$attr);
				//组合筛选id值、
				$num = count($attr);
				$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
				$this->assign('param',$param);
				//获取每一个筛选的gid
				foreach ($param as $v) {
					if($v){
						//自关联，相同的值进去乘积
						//进行自关联,param中的值只是taid中的一个值（分组过滤），不进行自关联只能查询此taid对于的gid，进行关联后可以查出taid对应的相同gavlue这一类的gid（关联条件是gvalue相同）
						$sGids[] = M()->join('__goods_attr__ as g1 JOIN __goods_attr__ g2 ON g1.gvalue=g2.gvalue')->where("g1.gaid={$v}")->getField('g2.goods_gid',true);
					}
				}
				//取gid的交集，及为符合所有筛选条件的gid的集合(层层取交集过滤)
				if(isset($sGids)){
					$finalGids = $sGids[0];
					foreach ($sGids as $v) {
						$finalGids = array_intersect($finalGids, $v);
					}
					//由于sGids拿到的是所有满足条件的gid并没有对cid进行过滤
					//与当前的cid下的gid集合在去交集
					$finalGids = array_intersect($finalGids, $gids);
	
				}else{
					//param全为零时，为所有产品cid
					$finalGids = $gids;
				}
			}else{
				//如果分类下没商品值，设置为空数组
				$finalGids = array();
			}
		}else{
			//如果为顶级分类$gids返回给$finalGids
			$finalGids = $gids;
		}
		

		//获取商品列表数据(属性和品牌信息要压入数组中循环，不可单独调取)
		if($finalGids){
			$listData = M('')->join('__goods__ as g JOIN __brand__ as b ON g.brand_bid = b.bid ')->where("gid IN (" . implode(',', $finalGids) .")")->all();
			//处理推荐、最新、最热的数据
			$order = Q('get.order',0,'intval');
			//规定$order==1为最新产品
			if($order==1){
				$listData = M('')->join('__goods__ as g JOIN __brand__ as b ON g.brand_bid = b.bid ')->where("gid IN (" . implode(',', $finalGids) .")")->order('addtime desc')->all();
			}
			//规定$order==2为最热产品
			if($order==2){
				$listData = M('')->join('__goods__ as g JOIN __brand__ as b ON g.brand_bid = b.bid ')->where("gid IN (" . implode(',', $finalGids) .")")->order('click desc')->all();
			}
			foreach($listData as $k => $v){
				$goodsAttr = M('')->join('__goods__ as g JOIN __goods_attr__ as ga ON g.gid = ga.goods_gid')->where("g.gid={$v['gid']} AND class=1")->all();
				foreach ($goodsAttr as $kk => $vv) {
					$listData[$k]['gvalue'][] = $vv['gvalue'];
				}
			}
			$this->assign('listData',$listData);
		}
		

		//调用导航方法
		$this->navigation();
		//处理分类
		$Topid = $this->getTopCid($cate, $cid);
		$cates = $this->cate();
		$topCate = array();
		foreach ($cates as $key => $value) {
			if($key==$Topid) $topCate = $value;
		}
		$this->assign('navCate',$topCate['son']);
		$this->assign('topCate',$topCate);
		$this->display();
	}



	/**
	 * 获取顶级cid
	 */
	private function getTopCid($data,$cid){
		static $Topid = 0;
		foreach ($data as $v) {
			if($v['cid']==$cid){
				$Topid = $v['cid'];
				$Topid = $this->getTopCid($data, $v['pid']);
			}
		}
		return $Topid;
	}

	/**
	 * 异步处理点赞
	 */
	public function ajaxLike(){
		if(!IS_AJAX) $this->error('非法请求');
		//判断用户是否存在，不存在返回一个0
		if(isset($_SESSION['uid'])){
			$gid = $_POST['gid'];
			$isLike = K('GoodsUser')->where("goods_gid={$gid} AND user_uid={$_SESSION['uid']}")->find();
			//判断此商品是否已被用户点赞，已点赞返回1
			if(!$isLike){
				//添加点赞
				$like = K('Goods')->updateLike($gid);
				echo $like;die;
				
			}else{
				echo -1;die;
			}
			
		}else{	
			echo 0;die;
		}
		
	}

	/**
	 * 全部分类模板
	 */
	public function allCate(){
		
		//处理分类
		//组合顶级和二级分类
		$dataCate = K('category')->where("pid=0")->all();
		foreach ($dataCate as $key => $value) {
			$dataCate[$key]['son'] = K('Category')->where("pid={$value['cid']}")->all();
		}
		$this->assign('dataCate',$dataCate);
		//调用导航方法
		$this->navigation();
		//随机调取商品
		$listData = M('')->join('__goods__ as g JOIN __brand__ as b ON g.brand_bid = b.bid ')->order('rand()')->all();
		//处理推荐、最新、最热的数据
		$order = Q('get.order',0,'intval');
		//规定$order==1为最新产品
		if($order==1){
			$listData = M('')->join('__goods__ as g JOIN __brand__ as b ON g.brand_bid = b.bid ')->order('addtime desc')->all();
		}
		//规定$order==2为最热产品
		if($order==2){
			$listData = M('')->join('__goods__ as g JOIN __brand__ as b ON g.brand_bid = b.bid ')->order('click desc')->all();
		}
		foreach($listData as $k => $v){
			$goodsAttr = M('')->join('__goods__ as g JOIN __goods_attr__ as ga ON g.gid = ga.goods_gid')->where("g.gid={$v['gid']} AND class=1")->all();
			foreach ($goodsAttr as $kk => $vv) {
				$listData[$k]['gvalue'][] = $vv['gvalue'];
			}
		}
		$this->assign('listData',$listData);
		$this->display();
	}
	
}



















 ?>